import {
    getSetting,
    chooseAddress,
    showModal
} from '../../utils/asyncWx.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
    data: {
        address: {},
        cart: [],
        allchecked: false,
        totalprice: 0,
        totalnum : 0
    },
    onShow() {
        /* 获取缓存中的数据 */
        const address = wx.getStorageSync('address');
        /* 获取缓存中的购物车数据 */
        const cart = wx.getStorageSync('cart') || []
        this.setData({address});
        this.setcart(cart);
    },
    /* 点击收货地址事件 */
    async handlechooseaddress() {
        let address = await chooseAddress();
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        wx.setStorageSync('address', address);
    },
    /* 商品的选中 */
    handlechecked(e) {
        /* 获取被修改的商品id */
        const goods_id = e.currentTarget.dataset.id;
        /* 获取购物车数组 */
        let {cart} = this.data;
        /* 找到被修改的商品对象 */
        let index = cart.findIndex(v => v.goods_id === goods_id);
        /* 选中状态取反 */
        cart[index].checked =! cart[index].checked;
        this.setcart(cart);
    },
    /* 设置购物车状态 同时重新计算 底部工具栏数据 */
    setcart(cart){
        let allchecked = true;
        let totalprice = 0;
        let totalnum = 0;
        cart.forEach(v => {
            if (v.checked) {
                totalprice += v.num * v.goods_price;
                totalnum += v.num;
            } else {
                allchecked = false
            }
        })
         /* 判断数组是否为空 */
        allchecked = cart.length ? allchecked : false;
        /* 重新设置回data和缓存中 */
        this.setData({
            cart,totalprice,totalnum,allchecked
        })
        wx.setStorageSync('cart', cart);
    },
    /* 改变全选 */
    handlechangeallchecked(){
        let {cart,allchecked}=this.data;
        allchecked=!allchecked;
        cart.forEach(v=>v.checked=allchecked);
        this.setcart(cart);

    },
    /* 商品数量编辑功能 */
    async handleItemNumEdit(e){
        /* 获取传递过来的参数 */
        const {operation,id}=e.currentTarget.dataset;
        /* 获取购物车数组 */
        let {cart}=this.data;
        /* 找到需要修改的商品的索引 */
        let index=cart.findIndex(v=>v.goods_id===id);
        /* 判断是否要执行删除 */
        if(cart[index].num===1&&operation===-1){
          const res=await showModal({content:"你是否要删除？"})
          if(res.confirm){
              cart.splice(index,1);
              this.setcart(cart);
          }
        }else{
            /* 进行数量的修改 */
            cart[index].num+=operation;
            /* 设置回缓存和data中 */
            this.setcart(cart);
        }
    },
    /* 点击结算 */
    handlePay(){
        //判断收货地址
        const {address,totalnum}=this.data;
        if(!address.userName){
            wx.showToast({
              title: '您还没有添加收货地址喔！',
              icon:'none'
            })
            return;
        }
        /* 判断商品数量是否为零 */
        if(totalnum===0){
            wx.showToast({
              title: '您还没有选购商品喔！',
              icon:'none'
            })
            return;
        }
        wx.navigateTo({
          url: '/pages/pay/pay',
        })
    }
})